package cn.tangwei.org.controller;

import cn.tangwei.basic.exception.BusinessException;
import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.org.domain.Shop;
import cn.tangwei.org.service.IShopService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺入驻接口",description = "店铺入驻接口详细描述")
public class ShopContorller {
    @Autowired
    private IShopService shopService;

    /**
     * 添加和修改
     * @param shop
     * @return
     */
    @PutMapping
    public AjaxRusult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一条
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.queryAll();
    }

    /**
     * 店铺入驻接口
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxRusult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxRusult.me();
        } catch (BusinessException e) { // 自定义业务异常，自己写了一个异常类BusinessException
            e.printStackTrace();
            return AjaxRusult.me().setMessage(e.getMessage());
        }catch (Exception e) {// 系统级别的异常
            e.printStackTrace();
            return AjaxRusult.me().setMessage("系统繁忙稍后重试");
        }

    }

}
